/**
 * 
 */
package com.itinsys.enma.dao.hibernate;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.itinsys.enma.dao.DBTemplate;

/**
 * @author pferraz
 * 
 */
public class DBObject {
	private static Logger logger = Logger.getLogger(DBObject.class);

	public DBObject() {
	}

	public static boolean addObject(Object o) {
		Session sess = null;
		Transaction tx = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			sess.save(o);
			tx.commit();
		} catch (HibernateException e) {
			// logger.error("Error al persistir objeto " + o.getClass(),e);
			logger.error("Error al persistir objeto " + o.getClass());
			tx.rollback();
			return false;
		} finally {
			try {
				if (sess != null) {
					sess.close();
				}
			} catch (Exception e) {
				logger.error(e);
			}
		}
		return true;
	}

	public static boolean updateObject(Object o) {
		Session sess = null;
		Transaction tx = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			sess.update(o);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			logger.error("Error al actualizar " + o.getClass());
			// logger.error("Error al actualizar "+ o.getClass(),e);
			tx.rollback();
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}

		}
	}

	public static boolean removeObject(Object o) {
		Session sess = null;
		Transaction tx = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			sess.delete(o);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			logger.error("Error al elminar dato", e);
			tx.rollback();
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
	}

	public static boolean txExecuteQuery(String strQuery) {

		Transaction tx = null;
		Session sess = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			Query query = sess.createSQLQuery(strQuery);
			query.executeUpdate();
			tx.commit();
		} catch (HibernateException e) {
			logger.error("Error al actualizar ", e);
			tx.rollback();
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}

		}
		return true;
	}

	public static boolean txInsertDelete(String sqlQuery) {
		Session session = DBTemplate.getConnTemplate().getSessionFactory().openSession();
		Transaction transaction = null;
		try {

			transaction = session.beginTransaction();
			Query query = session.createSQLQuery(sqlQuery);
			query.executeUpdate();
			session.getTransaction().commit();
			session.flush();
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;

	}
}
